package util;

import java.io.UnsupportedEncodingException;

public class Encoding {

	/**
	 * 转换编码，ISO-8859-1 ==> GBK
	 * @param str
	 * @return
	 */
	public static String isoToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("ISO-8859-1"), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("ISO-8859-1 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， GBK ==> ISO-8859-1 
	 * @param str
	 * @return
	 */
	public static String gbkToISO(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("GBK"), "ISO-8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(" GBK ==> ISO-8859-1 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，ISO-8859-1 ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String isoToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("ISO-8859-1 ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，系统默认编码 ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String strToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes(), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("系统默认编码 ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，系统默认编码 ==> GBK
	 * @param str
	 * @return
	 */
	public static String strToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes(), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("系统默认编码 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> ISO-8859-1
	 * @param str
	 * @return
	 */
	public static String utfToISO(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> ISO-8859-1 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> gb2312
	 * @param str
	 * @return
	 */
	public static String utfToGB(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "gb2312");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> gb2312 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> GBK
	 * @param str
	 * @return
	 */
	public static String utfToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， GBK ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String gbkToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("GBK"), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("GBK ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	
	
	/**
	 * 过滤HTML代码过滤掉，同时将换行和空格替换成HTML 的换行和空格
	 * @param str
	 * @return
	 */
	public static String htmlFilter(String str) {
		str = str.replace("<", "&lt;");
		str = str.replace(">", "&gt;");
		str = str.replace("\n", "<br/>");
		str = str.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
		str = str.replace(" ", "&nbsp;");
		str = str.replace("\'", "&#39;");
		return str;
	}
	
	/**
	 * 由于有些帖子是手机用户发表的，存在没有换行的，所以两个空格换行，\r\n
	 * @param str
	 * @return
	 */
	public static String strToHtml(String str) {
		str = str.replace("  ", "<br/>");
		str = str.replace("\r\n", "<br/>");
		str = str.replace("\n", "<br/>");
		str = str.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
		return str;
	}
	
	/**
	 * 测试
	 */
	/*public static void main(String[] args) {
		System.out.println(Encoding.htmlFilter("<javacript>alert(\"hello\");</javacript>\t\r\n\r\n"));
		String str = "你好啊  什么是最好的啊        fffff\tfdfaf \r\n \n";
		System.out.println(Encoding.strToHtml(str));
	}*/
}
